import {createRouter,createWebHistory} from 'vue-router'

//导入组件
//管理端
import LoginVue from '@/views/admin/LoginVue.vue'
import LayoutAdmin from '@/views/LayoutAdmin.vue'
import IndexAdmin from '@/views/admin/IndexPage.vue'
import NewsPage from '@/views/admin/NewsPage.vue'
import MembersPage from '@/views/admin/MembersPage.vue'
import HonorPage from '@/views/admin/HonorPage.vue'
import PaperPage from '@/views/admin/PaperPage.vue'
import ProjectPage from '@/views/admin/ProjectPage.vue'
import DFWPage from '@/views/admin/DFWPage.vue'
import AboutPage from '@/views/admin/AboutPage.vue'

//用户端
import LayoutUser from '@/views/LayoutUser.vue'
import IndexUser from '@/views/user/IndexVue.vue'
import News from '@/views/user/News.vue'
import TeacherInfo from '@/views/user/TeacherInfo.vue'
import StudentsInfo from '@/views/user/Students.vue'
import HonoursInfo from '@/views/user/HonorsInfo.vue'
import PapersInfo from '@/views/user/Papers.vue'
import Projects from '@/views/user/Projects.vue'
import Party from '@/views/user/Party.vue'
import About from '@/views/user/About.vue'
import DFW from '@/views/user/DFW.vue'
import NewsDetail from '@/views/user/NewsDetail.vue'



//定义路由关系
const routes = [
    {path:'/',component:LayoutUser,redirect: '/user/index/main',children:[
        {path:'user/index/main',component:IndexUser},
        {path:'user/News',component:News},
        {path:'user/teachers',component:TeacherInfo},
        {path:'user/students',component:StudentsInfo},
        {path:'user/honors',component:HonoursInfo},
        {path:'/user/papers',component:PapersInfo},
        {path:'/user/projects',component:Projects},
        {path:'/user/party',component:Party},
        {path:'/user/about',component:About},
        {path:'/user/dfw',component:DFW},
        {path:'/user/news/detail',component:NewsDetail},
    ]},
    {path:'/admin/Login',component:LoginVue},
    {path:'/admin/index',component:LayoutAdmin,redirect:'/admin/index/main',meta:{requiresAuth:true},children:[
        {path:'/admin/index/main',component:IndexAdmin,meta:{requiresAuth:true}},
        {path:'/admin/newsPage',component:NewsPage,meta:{requiresAuth:true}},
        {path:'/admin/memberPage',component:MembersPage,meta:{requiresAuth:true}},
        {path:'/admin/honorPage',component:HonorPage,meta:{requiresAuth:true}},
        {path:'/admin/paperPage',component:PaperPage,meta:{requiresAuth:true}},
        {path:'/admin/projectPage',component:ProjectPage,meta:{requiresAuth:true}},
        {path:'/admin/DFWPage',component:DFWPage,meta:{requiresAuth:true}},
        {path:'/admin/aboutPage',component:AboutPage,meta:{requiresAuth:true}}
    ]},
    


]

//创建路由器
const router = createRouter({
    history:createWebHistory(),
    routes:routes
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
    if (to.meta.requiresAuth) {
      const session = localStorage.getItem('session');
      if (session) {
        next();
      } else {
        // 跳转到登录页面，并记录目标路径
        next({ path: '/admin/Login', query: { redirect: to.fullPath } });
      }
    } else {
      next();
    }
  });

//导出路由
export default router